﻿using LYR_RPA.Properties;
using LYR_RPA.service;
using LYR_RPA.utils;
using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace LYR_RPA.pages.set
{
    /// <summary>
    /// BasicSetPage.xaml 的交互逻辑
    /// </summary>
    public partial class BasicSetPage : Page
    {
        private readonly WebSocketClient WebSocketClient;
        public BasicSetPage()
        {
            InitializeComponent();
            WebSocketClient = WebSocketClient.Instance;
            UIRobotPath.Text = Settings.Default.RobotPath;
            if (!string.IsNullOrEmpty(Settings.Default.ServerAddress))
            {
                ServerAddress.Text= Settings.Default.ServerAddress;
                
            }
            if(!string.IsNullOrEmpty(Settings.Default.Port))
            {
                Port.Text= Settings.Default.Port;
            }

            if (!string.IsNullOrEmpty(Settings.Default.Username))
            {
                Username.Text = Settings.Default.Username;
            }

            if (!string.IsNullOrEmpty(Settings.Default.Password))
            {
                Password.Text = Settings.Default.Password;
            }
        }

        private void SelectFileButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Title = "选择文件",
                Filter = "应用程序 (*UiRobot.exe*)|*.exe*" // 可以根据需要设置文件类型过滤
            };

            bool? result = openFileDialog.ShowDialog();

            if (result == true)
            {
                string selectedFilePath = openFileDialog.FileName;
                Settings.Default.RobotPath = selectedFilePath;
                Settings.Default.Save();
                UIRobotPath.Text = Settings.Default.RobotPath;
            }
        }

        private void Restart_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.RobotPath = App.RobotPath;
            Settings.Default.Save();
            UIRobotPath.Text = Settings.Default.RobotPath;
        }

        private async void Determine_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(ServerAddress.Text) || string.IsNullOrEmpty(Port.Text))
            {
                Settings.Default.ServerAddress = ServerAddress.Text;
                Settings.Default.Port = Port.Text;

                Settings.Default.Save();
                _=WebSocketClient.CloseWebSocketAsync();
            }
            else
            {
                Settings.Default.ServerAddress = ServerAddress.Text;
                Settings.Default.Port = Port.Text;
                Settings.Default.Username = Username.Text;
                Settings.Default.Password = Password.Text;
                Settings.Default.Save();
                string token=await HttpClientUtils.HttpClient().Login(ServerAddress.Text, Port.Text, Username.Text, Password.Text);
                WebSocketClient.token=token;
                Console.WriteLine(token);
                SnackbarThree.Background = Brushes.Red;
                var messageQueue = SnackbarThree.MessageQueue;

                // 显示消息
                var message = "连接失败,正在重连...！";
                
                if (!string.IsNullOrEmpty(token))
                {
                    await WebSocketClient.Connect();
                    if (WebSocketClient.IsConnected())
                    {
                        SnackbarThree.Background = Brushes.Green;
                        message = "连接成功！";
                    }
                }
                messageQueue.Enqueue(message);
            }
            
        }
    }
}
